﻿'
' Will Strohl (will.strohl@gmail.com)
' http://www.willstrohl.com
'
'Copyright (c) 2011, Will Strohl
'All rights reserved.
'
'Redistribution and use in source and binary forms, with or without modification, are 
'permitted provided that the following conditions are met:
'
'Redistributions of source code must retain the above copyright notice, this list of 
'conditions and the following disclaimer.
'
'Redistributions in binary form must reproduce the above copyright notice, this list 
'of conditions and the following disclaimer in the documentation and/or other 
'materials provided with the distribution.
'
'Neither the name of Will Strohl nor the names of its contributors may be 
'used to endorse or promote products derived from this software without specific prior 
'written permission.
'
'THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
'EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
'OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
'SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
'INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
'TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
'BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
'CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
'ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 
'DAMAGE.
'

Imports DotNetNuke.Entities.Modules
Imports DotNetNuke.Services.Localization
Imports System.Web.UI

Namespace WillStrohl.Modules.CodeCamp

    Public MustInherit Class WnsPortalModuleBase
        Inherits PortalModuleBase

#Region " Private Members "

        Private Const URL_HTTPS As String = "https://"
        Private Const URL_HTTP As String = "http://"
        Private Const SERVER_NAME As String = "SERVER_NAME"
        Private Const BLANK As String = "[NULLVALUETOKEN]"
        Private Const SCRIPT_BLOCK As String = "<script language=""javascript"" type=""text/javascript"">/*<[CDATA[*/ {0} /*]]>*/</script> "
        Private Const SCRIPT_TAG As String = "<script language=""javascript"" type=""text/javascript"" src=""{0}""></script>"
        Private Const CSS As String = "<link rel=""stylesheet"" type=""text/css"" href=""{0}"" />"

        Private p_PageTitle As String = String.Empty
        Private p_PageDescription As String = String.Empty
        Private p_PageKeywords As String = String.Empty

#End Region

#Region " Properties "

        ''' <summary>
        ''' ModulePathFull - this property returns the ModulePath property with the rest of the URL appended.
        ''' </summary>
        ''' <value>String</value>
        ''' <returns>String (read-only) - The URL path for the module</returns>
        ''' <remarks>
        ''' ModulePathFull - this property returns the ModulePath property with the rest of the URL appended.
        ''' </remarks>
        ''' <history>
        ''' </history>
        Public ReadOnly Property ModulePathFull() As String
            Get
                If Request.IsSecureConnection Then
                    Return String.Concat(URL_HTTPS, Request.ServerVariables(SERVER_NAME), Me.ControlPath)
                Else
                    Return String.Concat(URL_HTTP, Request.ServerVariables(SERVER_NAME), Me.ControlPath)
                End If
            End Get
        End Property

#End Region

#Region " Localization "

        ''' <summary>
        ''' GetLocalizedString - Gets the language-specific text from the DNN localization files.
        ''' </summary>
        ''' <param name="LocalizationKey">String - the key name of the text you want to retrieve</param>
        ''' <returns></returns>
        ''' <remarks>
        ''' This is an extension of standard DNN functionality.
        ''' </remarks>
        ''' <history>
        ''' </history>
        Protected Overloads Function GetLocalizedString(ByVal LocalizationKey As String) As String
            If Not String.IsNullOrEmpty(LocalizationKey) Then
                Return Localization.GetString(LocalizationKey, Me.LocalResourceFile)
            Else
                Return String.Empty
            End If
        End Function

        ''' <summary>
        ''' GetLocalizedString - Gets the language-specific text from the DNN localization files.
        ''' </summary>
        ''' <param name="LocalizationKey">String - the key name of the text you want to retrieve</param>
        ''' <param name="LocalResourceFilePath">String - the local server path to the localization file</param>
        ''' <returns></returns>
        ''' <remarks>
        ''' This is an extension of standard DNN functionality.
        ''' </remarks>
        ''' <history>
        ''' </history>
        Protected Overloads Function GetLocalizedString(ByVal LocalizationKey As String, ByVal LocalResourceFilePath As String) As String
            If Not String.IsNullOrEmpty(LocalizationKey) Then
                Return Localization.GetString(LocalizationKey, LocalResourceFilePath)
            Else
                Return String.Empty
            End If
        End Function

#End Region

#Region " Page Meta Data "

        ''' <summary>
        ''' OverridePageMetaData - this method overrides the page (tab) meta data.
        ''' </summary>
        ''' <param name="Keywords">String - the page keywords (comma delimited)</param>
        ''' <remarks>
        ''' POSSIBLE EXCEPTIONS:
        ''' ArgumentNullException - this will occur should any of the arguments contain a null or empty value.
        ''' </remarks>
        ''' <history>
        ''' </history>
        Protected Overloads Sub OverridePageMetaData(ByVal Keywords As String)
            OverridePageMetaData(BLANK, Keywords, BLANK)
        End Sub

        ''' <summary>
        ''' OverridePageMetaData - this method overrides the page (tab) meta data.
        ''' </summary>
        ''' <param name="Keywords">String - the page keywords (comma delimited)</param>
        ''' <param name="Description">String - the page description</param>
        ''' <remarks>
        ''' POSSIBLE EXCEPTIONS:
        ''' ArgumentNullException - this will occur should any of the arguments contain a null or empty value.
        ''' </remarks>
        ''' <history>
        ''' </history>
        Protected Overloads Sub OverridePageMetaData(ByVal Keywords As String, ByVal Description As String)
            OverridePageMetaData(BLANK, Keywords, Description)
        End Sub

        ''' <summary>
        ''' OverridePageMetaData - this method overrides the page (tab) meta data.
        ''' </summary>
        ''' <param name="Title">String - the page title</param>
        ''' <param name="Keywords">String - the page keywords (comma delimited)</param>
        ''' <param name="Description">String - the page description</param>
        ''' <remarks>
        ''' POSSIBLE EXCEPTIONS:
        ''' ArgumentNullException - this will occur should any of the arguments contain a null or empty value.
        ''' </remarks>
        ''' <history>
        ''' </history>
        Protected Overloads Sub OverridePageMetaData(ByVal Title As String, ByVal Keywords As String, ByVal Description As String)
            ' 
            ' BEGIN VALIDATION
            '

            If String.IsNullOrEmpty(Title) Then
                Throw New ArgumentNullException("OverridePageMetaData")
            End If
            If String.IsNullOrEmpty(Keywords) Then
                Throw New ArgumentNullException("OverridePageMetaData")
            End If
            If String.IsNullOrEmpty(Description) Then
                Throw New ArgumentNullException("OverridePageMetaData")
            End If

            '
            ' END VALIDATION
            '

            Me.p_PageTitle = Title
            Me.p_PageKeywords = Keywords
            Me.p_PageDescription = Description

            Dim currentPage As DotNetNuke.Framework.CDefault
            currentPage = CType(Me.Page, DotNetNuke.Framework.CDefault)

            If Not String.Equals(Me.p_PageTitle, BLANK) Then
                currentPage.Title = Me.p_PageTitle
            End If

            If Not String.Equals(Me.p_PageDescription, BLANK) Then
                currentPage.Description = Me.p_PageDescription
            End If

            If Not String.Equals(Me.p_PageKeywords, BLANK) Then
                currentPage.KeyWords = Me.p_PageKeywords
            End If

        End Sub

#End Region

#Region " Client Script "

        ''' <summary>
        ''' GetClientScript - returns a formatted script HTML tag with the given path
        ''' </summary>
        ''' <param name="ScriptPath">String - the resolvable path to the script file</param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        ''' <history>
        ''' </history>
        Protected Function GetClientScript(ByVal ScriptPath As String) As String

            If Not String.IsNullOrEmpty(ScriptPath) Then
                Return String.Format(SCRIPT_TAG, ScriptPath)
            Else
                Return String.Empty
            End If

        End Function

        ''' <summary>
        ''' GetClientScriptBlock - this method returns a script block with the desired JavaScript snippet already inserted into it.
        ''' </summary>
        ''' <param name="Script">String - the JavaScript snippet to be inserted between the HTML Script tags</param>
        ''' <returns>String - the JavaScript enclosed within Script HTML tags.</returns>
        ''' <remarks></remarks>
        ''' <history>
        ''' </history>
        Protected Function GetClientScriptBlock(ByVal Script As String) As String
            Return String.Format(SCRIPT_BLOCK, Script)
        End Function

        ''' <summary>
        ''' RegisterClientScript - This method adds a script reference to the page
        ''' </summary>
        ''' <param name="Key">String - a unique key assigned to the script reference</param>
        ''' <param name="Path">String - the resolveable URL to the script file</param>
        ''' <remarks></remarks>
        ''' <history>
        ''' </history>
        Protected Sub RegisterClientScript(ByVal Key As String, ByVal Path As String)

            If Not Me.Page.ClientScript.IsClientScriptBlockRegistered(Key) Then

                Me.Page.ClientScript.RegisterClientScriptBlock(Me.Page.GetType, Key, Me.GetClientScript(Path))

            End If

        End Sub

#End Region

#Region " Events "

        Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

            ' request that the DNN framework load the jQuery script into the markup
            DotNetNuke.Framework.jQuery.RequestDnnPluginsRegistration()

        End Sub

#End Region

        ''' <summary>
        ''' AddContentToHeader - This method allows you to add content to the header of the parent page.
        ''' </summary>
        ''' <param name="Content">String - the content to be added to the header.</param>
        ''' <remarks>
        ''' This method is meant to add things like scripts, meta data, and more.
        ''' </remarks>
        ''' <history>
        ''' </history>
        Protected Sub AddContentToHeader(ByVal Content As String)
            Me.Parent.Page.Header.Controls.Add(New LiteralControl(Content))
        End Sub

        ''' <summary>
        ''' RegisterCSS - This method adds a CSS reference to the page
        ''' </summary>
        ''' <param name="Key">String - a unique key assigned to the script reference</param>
        ''' <param name="Path">String - the resolveable URL to the script file</param>
        ''' <remarks></remarks>
        ''' <history>
        ''' </history>
        Protected Sub RegisterCSS(ByVal Key As String, ByVal Path As String)

            'Me.Page.Header.Controls.Add(New LiteralControl("<link rel=""stylesheet"" type=""text/css"" href=""http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/themes/ui-lightness/jquery-ui.css"" />"))
            Me.AddContentToHeader(String.Format(CSS, Path))

        End Sub

    End Class

End Namespace